<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once './mode/database.php';
?>

<h1>Truong Cong Thuc</h1>
<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
?>
<form method="POST">
    Min fans<input type="text" name="min_fans"><br>
    Max fans<input type="text" name="max_fans"><br>
    Keyword <input type="text" name="keyword"><br>
    Company<select name="id_company">
<?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select>
 
    <input type="submit" name="submit" value="Enter">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans']; //TODO
    $max_fans = $_POST['max_fans']; //TODO
    $keyword = $_POST['keyword']; //TODO
    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                      
                  LIMIT 0, 10
                  ";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
             WHERE (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else {
        $query = "SELECT * FROM pages    
                  WHERE
                      (likes >= $min_fans)
                      AND (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    }


    $pages = getData($query);
    ?>

    <?php if (!empty($pages)): ?>
        <table>
            <tr>
                <th>ID</th>
                <th>Picture</th>
                <th>Pid</th>
                <th> description </th>
                <th>Name</th>
                <th>Username</th>
                <th>Phone</th>
                <th>Like</th>
                <th>link</th>
            </tr>
        <?php foreach ($pages as $page): ?>
                <tr>
                    <td><?php echo $page['pid'] ?></td>
                    <td><img src='<?php echo $page['picture'] ?>'></td>
                    <td><?php echo $page['id'] ?></td>
                    <td><?php echo substr($page['description'], 0, 100) ?></td>
                    <td><?php echo $page['name'] ?></td>
                    <td><?php echo $page['username'] ?></td>
                    <td><?php echo $page['phone'] ?></td>
                    <td><?php echo $page['likes'] ?></td> 
            <?php // var_dump($page);die(); ?>
                </tr>
        <?php endforeach; ?>
        </table> 



    <?php endif; ?>
        <?php endif; ?>
